package com.jingdianjichi.redis.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {

    // Spring 应用上下文
    private static ApplicationContext applicationContext;

    // 实现 ApplicationContextAware 接口的方法，用于注入 Spring 应用上下文
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 将注入的 Spring 应用上下文赋值给静态变量 applicationContext
        this.applicationContext = applicationContext;
    }

    // 获取 Spring 应用上下文
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    // 根据类型获取 Bean
    public static Object getBean(Class type) {
        // 从 Spring 上下文中根据类型获取 Bean 实例并返回
        return applicationContext.getBean(type);
    }
}
